﻿
using System;
using System.Diagnostics.Contracts;
using System.Linq;
using Adalbertus.BudgetPlanner.Domain;
using Caliburn.Micro;

namespace Adalbertus.BudgetPlanner.ViewModels.ExternalSources
{
    public class IncomeVM : UnitOfWorkVM
    {
        private Income _wrappedIncome;
        public Income WrappedIncome
        {
            get
            {
                Contract.Ensures(Contract.Result<Income>() != null);
                return _wrappedIncome;
            }
        }
        public IncomeVM(Income income)
        {
            Contract.Requires<ArgumentNullException>(income != null);
            _wrappedIncome = income;
            _entries = new BindableCollection<IncomeEntryVM>();
            _entries.AddRange(WrappedIncome.Entries.Select(x => new IncomeEntryVM(x, this)).ToList());
        }

        public string Name
        {
            get { return WrappedIncome.Name; }
            set
            {
                WrappedIncome.Name = value;
                IsDirty = true;
                NotifyOfPropertyChange(() => Name);
            }
        }

        private BindableCollection<IncomeEntryVM> _entries;
        public BindableCollection<IncomeEntryVM> Entries
        {
            get
            {
                Contract.Ensures(Contract.Result<BindableCollection<IncomeEntryVM>>() != null);
                return _entries;
            }
        }

        internal void AddEntry(IncomeEntry incomeEntry)
        {
            Contract.Requires<ArgumentNullException>(incomeEntry != null);

            IncomeEntryVM entryVm = new IncomeEntryVM(incomeEntry, this);
            Entries.Add(entryVm);
        }

        internal void RemoveEntry(IncomeEntryVM incomeEntry)
        {
            Contract.Requires<ArgumentNullException>(incomeEntry != null);

            Entries.Remove(incomeEntry);
        }
    }
}
